package com.example.myapplication.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapAdapter extends ArrayAdapter<HashMap> {
    private int resourceId;
    public MapAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.my_text_view, parent,false);
            holder = new ViewHolder();
            holder.setTextView(view.findViewById(R.id.showText));
            view.setTag(holder);
            return view;
        }
        view = convertView;
        holder = (ViewHolder) view.getTag();
        holder.getTextView().setText((String) getItem(position).get("name"));
        return view;

    }

    public void setData(List<HashMap> data) {
        this.clear();
        this.addAll(data);
        
    }

    public List getData() {
        int count = this.getCount();
        if (count <= 0) {
            return null;
        }
        List data = new ArrayList(count);
        for (int i = 0; i < count; i++) {
            data.add(this.getItem(i));
        }
        return data;
    }

    class ViewHolder{
        private TextView textView;

        public TextView getTextView() {
            return textView;
        }

        public void setTextView(TextView textView) {
            this.textView = textView;
        }
    }
}
